{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Difference Between Element Sum and Digit Sum of an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: differenceOfSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组元素和与数字和的绝对差"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个正整数数组 <code>nums</code> 。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>元素和</strong> 是 <code>nums</code> 中的所有元素相加求和。</li>\n",
    "\t<li><strong>数字和</strong> 是&nbsp;<code>nums</code> 中每一个元素的每一数位（重复数位需多次求和）相加求和。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回 <strong>元素和</strong> 与 <strong>数字和</strong> 的绝对差。</p>\n",
    "\n",
    "<p><strong>注意：</strong>两个整数 <code>x</code> 和 <code>y</code> 的绝对差定义为 <code>|x - y|</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,15,6,3]\n",
    "<strong>输出：</strong>9\n",
    "<strong>解释：</strong>\n",
    "nums 的元素和是 1 + 15 + 6 + 3 = 25 。\n",
    "nums 的数字和是 1 + 1 + 5 + 6 + 3 = 16 。\n",
    "元素和与数字和的绝对差是 |25 - 16| = 9 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>\n",
    "nums 的元素和是 1 + 2 + 3 + 4 = 10 。\n",
    "nums 的数字和是 1 + 2 + 3 + 4 = 10 。\n",
    "元素和与数字和的绝对差是 |10 - 10| = 0 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 2000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 2000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [difference-between-element-sum-and-digit-sum-of-an-array](https://leetcode.cn/problems/difference-between-element-sum-and-digit-sum-of-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [difference-between-element-sum-and-digit-sum-of-an-array](https://leetcode.cn/problems/difference-between-element-sum-and-digit-sum-of-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,15,6,3]', '[1,2,3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in nums:\n",
    "            s = i\n",
    "            a = 0\n",
    "            while i > 9:\n",
    "                i = i//10\n",
    "                a += i%10\n",
    "                # print(a)\n",
    "            if a>0:\n",
    "                s=s//10\n",
    "                s=s*10\n",
    "                # print(s)\n",
    "                ans+=s-a\n",
    "        return ans\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        yuansu = 0\n",
    "        shuzi = 0\n",
    "        for i in nums:\n",
    "            yuansu += i\n",
    "            for s in str(i):\n",
    "                shuzi += int(s)\n",
    "        return abs(yuansu - shuzi)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        elesum = 0\n",
    "        numsum = 0\n",
    "        for i in nums:\n",
    "            elesum += i\n",
    "        \n",
    "        for i in nums:\n",
    "            numstr = str(i)\n",
    "            for j in range(len(numstr)):\n",
    "                numsum += int(numstr[j])\n",
    "        \n",
    "        if elesum >= numsum:\n",
    "            return elesum - numsum\n",
    "        else:\n",
    "            return numsum - elesum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            sum_num = 0\n",
    "            num = x\n",
    "            while num:\n",
    "                sum_num += num % 10\n",
    "                num //= 10\n",
    "            ans += abs(x - sum_num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        a = 0\n",
    "        for i in nums:\n",
    "            a += i\n",
    "            while i > 0:\n",
    "                a -= i%10\n",
    "                i //= 10\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        sum1=0\n",
    "        for i in nums:\n",
    "            sum1=sum1+sum([int(j) for j in list(str(i))]) if i>9 else sum1+i\n",
    "        return sum(nums)-sum1 if sum(nums)>sum1 else sum1-sum(nums)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        result = 0\n",
    "        for num in nums:\n",
    "            if num >= 10:\n",
    "                if num < 100:\n",
    "                    result += num - num // 10 - num % 10\n",
    "                elif num < 1000:\n",
    "                    result += num - num // 100 - (num//10) % 10 - num % 10\n",
    "                else:\n",
    "                    result += num - num // 1000 - (num//100) % 10 - (num//10) % 10 - num % 10\n",
    "            else:\n",
    "                result +=0\n",
    "        \n",
    "        return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return sum([num - sum([int(x) for x in list(str(num))]) for num in nums])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        num_1 = 0\n",
    "        num_2 = 0\n",
    "        for i in nums:\n",
    "            num_1 += i\n",
    "            if i < 10:\n",
    "                num_2 += i\n",
    "            else:\n",
    "                for j in range(len(str(i))):\n",
    "                    num_2 += int(str(i)[j])\n",
    "        return abs(num_1 - num_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        sum1,sum2=0,sum(nums)\n",
    "        for i in nums:\n",
    "            sum1=sum1+sum([int(j) for j in list(str(i))]) if i>9 else sum1+i\n",
    "        return abs(sum2-sum1)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans += x\n",
    "            while x:\n",
    "                ans -= x % 10\n",
    "                x //= 10\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        s,n = 0,0\n",
    "        for x in nums:\n",
    "            n += x\n",
    "            while x:\n",
    "                s += x % 10\n",
    "                x = x//10\n",
    "        return n - s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        for num in nums:\n",
    "            a += num\n",
    "            while num > 0:\n",
    "                b += num % 10\n",
    "                num //= 10\n",
    "\n",
    "        return abs(a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        total_1 = 0\n",
    "        total_2 = 0\n",
    "        for num in nums:\n",
    "            total_1 += num\n",
    "            if num < 10:\n",
    "                total_2 += num\n",
    "            else:\n",
    "                num_str = str(num)\n",
    "                for s in num_str:\n",
    "                    total_2 += int(s)\n",
    "        return total_1 - total_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def num_sum(self,num):\n",
    "        res=0\n",
    "        while num:\n",
    "            res+=(num%10)\n",
    "            num//=10\n",
    "        return res\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for num in nums:\n",
    "            ans+=(num-self.num_sum(num))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans += x\n",
    "            while x:\n",
    "                ans -= x % 10\n",
    "                x //= 10\n",
    "        return ans\n",
    "        # a = sum(nums)\n",
    "        # b = 0\n",
    "        # for num in nums:\n",
    "        #     tmp = list(map(int, str(num)))\n",
    "        #     b += sum(tmp)\n",
    "        # return abs(a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        \n",
    "        sum1 = 0\n",
    "        for x in nums:\n",
    "            sum1+=x\n",
    "            while x:\n",
    "                sum1 -= x%10\n",
    "                x //= 10\n",
    "        return sum1\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        diff = 0\n",
    "        for i in range(len(nums)):\n",
    "            str_num = 0\n",
    "            for ele in str(nums[i]):\n",
    "                str_num += int(ele)\n",
    "\n",
    "            diff += nums[i] - str_num\n",
    "\n",
    "        return abs(diff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        num_1 = 0\n",
    "        num_2 = 0\n",
    "        for i in nums:\n",
    "            num_1 += i\n",
    "            if i < 10:\n",
    "                num_2 += i\n",
    "            else:\n",
    "                for j in range(len(str(i))):\n",
    "                    num_2 += int(str(i)[j])\n",
    "        if (num_1 - num_2) < 0:\n",
    "            return num_2 - num_1\n",
    "        else:\n",
    "            return num_1 - num_2\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        sum1 = sum(nums)\n",
    "        sum2 = 0\n",
    "        for num in nums:\n",
    "            while num:\n",
    "                sum2 += num%10\n",
    "                num //= 10\n",
    "        return abs(sum1 - sum2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def num_sum(self,num):\n",
    "        res=0\n",
    "        k=1\n",
    "        while num:\n",
    "            res+=(num%10*(k-1))\n",
    "            k*=10\n",
    "            num//=10\n",
    "        return res\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for num in nums:\n",
    "            ans+=self.num_sum(num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in nums:\n",
    "            ans+=i\n",
    "            while i:\n",
    "                ans-=i%10\n",
    "                i//=10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        num_sum=sum(nums)\n",
    "        print(num_sum)\n",
    "        num_a=0\n",
    "        for i in nums:\n",
    "            a=str(i)\n",
    "            for j in a:\n",
    "                num_a=num_a+int(j)\n",
    "        print(num_a)\n",
    "        if (num_sum-num_a)>0:\n",
    "            return num_sum-num_a\n",
    "        else:\n",
    "            return num_a-num_sum\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        s1, s2 = 0, 0\n",
    "        for n in nums:\n",
    "            a = 0\n",
    "            for c in str(n):\n",
    "                a += int(c)\n",
    "            s1 += a\n",
    "            s2 += n\n",
    "        return abs(s1 - s2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        s = sum(nums)\n",
    "        t = sum(chain.from_iterable(map(int, str(n)) for n in nums))\n",
    "        return abs(s -t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        a = 0\n",
    "        b = 0\n",
    "        for num in nums:\n",
    "            a += num\n",
    "            while num > 0:\n",
    "                b += num % 10\n",
    "                num //= 10\n",
    "\n",
    "        return abs(a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        count1, count2 = 0, 0\n",
    "\n",
    "        for num in nums:\n",
    "            count1+=num\n",
    "            digits = str(num)\n",
    "            for i in range(len(digits)):\n",
    "                count2+=int(digits[i])\n",
    "        \n",
    "        return abs(count1 - count2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        esum, bsum = 0,0\n",
    "        for num in nums:\n",
    "            esum += num\n",
    "            while num!=0:\n",
    "                bsum += (num%10)\n",
    "                num = num//10\n",
    "        return abs(esum - bsum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        num_1 = 0\n",
    "        num_2 = 0\n",
    "        for i in nums:\n",
    "            num_1 += i\n",
    "            if i < 10:\n",
    "                num_2 += i\n",
    "            else:\n",
    "                for j in range(len(str(i))):\n",
    "                    num_2 += int(str(i)[j])\n",
    "        return abs(num_1 - num_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums)-sum(int(j) for i in map(str,nums) for j in str(i)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in nums:\n",
    "            ans+=i\n",
    "            for j in str(i):\n",
    "                ans-=int(j)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans += x\n",
    "            while x:\n",
    "                ans -= x % 10\n",
    "                x //= 10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums) - sum([sum([int(k) for k in str(v)]) for v in nums]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans += x\n",
    "            while x:\n",
    "                ans -= x % 10\n",
    "                x //= 10\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        sume=sumn=0\n",
    "        for i in nums:\n",
    "            sume+=i\n",
    "            for j in str(i):\n",
    "                sumn+=int(j)\n",
    "        return sume-sumn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        num_1 = 0\n",
    "        num_2 = 0\n",
    "        for i in nums:\n",
    "            num_1 += i\n",
    "            if i < 10:\n",
    "                num_2 += i\n",
    "            else:\n",
    "                for j in range(len(str(i))):\n",
    "                    num_2 += int(str(i)[j])\n",
    "        if (num_1 - num_2) < 0:\n",
    "            return num_2 - num_1\n",
    "        else:\n",
    "            return num_1 - num_2\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        Sum = sum(nums)\n",
    "        digit_sum = 0\n",
    "        for num in nums:\n",
    "            while num:\n",
    "                digit_sum += num % 10\n",
    "                num = num // 10\n",
    "        \n",
    "        if Sum > digit_sum:\n",
    "            return Sum - digit_sum\n",
    "        return digit_sum - Sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            while x:\n",
    "                ans += x % 10\n",
    "                x //= 10\n",
    "        return abs(ans - sum(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum([sum(int(c) for c in str(n)) for n in nums]) - sum(nums))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        num_1 = 0\n",
    "        num_2 = 0\n",
    "        for i in nums:\n",
    "            num_1 += i\n",
    "            if i < 10:\n",
    "                num_2 += i\n",
    "            else:\n",
    "                for j in range(len(str(i))):\n",
    "                    num_2 += int(str(i)[j])\n",
    "        if (num_1 - num_2) < 0:\n",
    "            return num_2 - num_1\n",
    "        else:\n",
    "            return num_1 - num_2\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        s1, s2 = 0, 0\n",
    "        for n in nums:\n",
    "            s1 += sum(map(int, str(n)))\n",
    "            s2 += n\n",
    "        return abs(s1 - s2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        p=0\n",
    "        for num in nums:\n",
    "            p+=num\n",
    "            while num:\n",
    "                p-=num%10\n",
    "                num//=10\n",
    "        return p\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        cc1 = 0\n",
    "        cc2 = 0\n",
    "        for x in nums:\n",
    "            cc1 += x\n",
    "            while x>0:\n",
    "                k, v = divmod(x,10)\n",
    "                cc2 += v \n",
    "                x = k \n",
    "        return abs(cc1-cc2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        def f(x):\n",
    "            a=0\n",
    "            for i in str(x):\n",
    "                a+=int(i)\n",
    "            return a\n",
    "        e_sum = sum(nums)\n",
    "        d_sum = sum(f(item) for item in nums)\n",
    "        return abs(e_sum-d_sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        s1, s2 = 0, 0\n",
    "        for n in nums:\n",
    "            s2 += n\n",
    "            s2 -= sum(map(int, str(n)))\n",
    "        return s2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        nn=[]\n",
    "        for i in nums:\n",
    "            nn.extend([int(i) for i in list(str(i))])\n",
    "        return abs(sum(nums)-sum(nn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        for x in nums:\n",
    "            ans += x\n",
    "            while x:\n",
    "                ans -= x % 10\n",
    "                x //= 10\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        esum, bsum = 0,0\n",
    "        for num in nums:\n",
    "            esum += num\n",
    "            bsum += (sum([int(e) for e in str(num)]))\n",
    "        return abs(esum - bsum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return sum(nums)-sum(sum(map(int,str(n))) for n in nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        result1 = sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = str(nums[i])\n",
    "        b = \"\".join(nums)\n",
    "        result2 = 0\n",
    "        for i in b:\n",
    "            result2 += int(i)\n",
    "        return int(fabs(result1-result2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        elementSum=0\n",
    "        numSum=0\n",
    "        for i in nums:\n",
    "            elementSum+=i\n",
    "            for j in str(i):\n",
    "                numSum+=int(j)\n",
    "        return abs(elementSum-numSum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        result1 = sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = str(nums[i])\n",
    "        b = \"\".join(nums)\n",
    "        result2 = 0\n",
    "        for i in b:\n",
    "            result2 += int(i)\n",
    "        return int(fabs(result1-result2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums):\n",
    "        ans = []\n",
    "        sums = 0\n",
    "        minsums = 0\n",
    "        for i in nums:\n",
    "            sums += i\n",
    "            ans.append(str(i))\n",
    "        strs = ''.join(ans)\n",
    "        for j in str(strs):\n",
    "            minsums += eval(j)\n",
    "        return sums - minsums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        ans=0\n",
    "        for i in nums:\n",
    "            ans+=i\n",
    "            for j in str(i):\n",
    "                ans-=int(j)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums)-sum(map(int,\"\".join(map(str,nums)))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        sums = 0\n",
    "        numsums = 0\n",
    "        ## 元素和\n",
    "        for i in nums:\n",
    "            sums += i\n",
    "        # 数字和\n",
    "        templist = []\n",
    "        for i in nums:\n",
    "            if i<10:\n",
    "                templist.append(i)\n",
    "            else:\n",
    "                temp = str(i)\n",
    "                for j in range(len(temp)):\n",
    "                    templist.append(int(temp[j]))\n",
    "        for i in templist:\n",
    "            numsums += i\n",
    "        return abs(sums-numsums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\r\n",
    "        return abs(sum(nums) - sum(sum(int(c) for c in str(i)) for i in nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        result1 = sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = str(nums[i])\n",
    "        b = \"\".join(nums)\n",
    "        result2 = 0\n",
    "        for i in b:\n",
    "            result2 += int(i)\n",
    "        return int(fabs(result1-result2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums)-sum(map(int, \"\".join(map(str, nums)))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums)-sum([int (i) for i in ''.join([str(j) for j in nums])]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        elesum = sum(nums)\n",
    "\n",
    "        nums = list(map(str, nums))\n",
    "        nums = ''.join(nums)\n",
    "        nums = list(map(int,nums))\n",
    "\n",
    "        return abs(sum(nums) - elesum)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        x, y = 0, 0\n",
    "        str_nums = ''.join([str(_) for _ in nums])\n",
    "        x = sum(nums)\n",
    "        for _ in str_nums:\n",
    "            y += int(_)\n",
    "        return abs(x-y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums) - sum(map(int, \"\".join(map(str, nums)))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\r\n",
    "        s = sum(nums)\r\n",
    "        s2 = sum([sum([int(a) for a in str(x)]) for x in nums])\r\n",
    "        return abs(s - s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def differenceOfSum(self, nums: List[int]) -> int:\n",
    "        return abs(sum(nums) - sum(map(int, list(''.join(map(str, nums))))))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
